<template>
	<el-card class="library-manage">
		<!-- 头部区域：标题和右侧新增按钮 -->
		<div class="header-container">
			<div class="header-left">
				<h2>对比库文件管理</h2>
			</div>
			<div class="header-right">
				<el-button type="primary" @click="openAddPage">新增库文件</el-button>
			</div>
		</div>
		<!-- 库文件列表区域 -->
		<div class="table-section">
			<el-table :data="libraryFilesTransformed" style="width: 100%" border>
				<el-table-column type="index" label="序号" width="80"></el-table-column>
				<el-table-column prop="fileName" label="文件名称"></el-table-column>
			</el-table>
		</div>
	</el-card>
</template>

<script>
	import {
		ref,
		reactive,
		computed,
		onMounted
	} from 'vue'
	import {
		ElMessage
	} from 'element-plus'
	export default {
		name: 'LibraryManage',
		setup() {
			const libraryFiles = ref([])

			// 直接展示返回的文件名称
			const libraryFilesTransformed = computed(() =>
				libraryFiles.value.map(name => ({
					fileName: name
				}))
			)

			// 查询库文件接口请求
			const fetchLibraryFiles = () => {
				uni.request({
					url: 'http://120.26.38.251/cat/v1/fileMgr/qryBaseFiles',
					method: 'POST',
					success: (res) => {
						if (res.data && res.data.code === '200') {
							libraryFiles.value = res.data.result || []
						} else {
							ElMessage.error(res.data?.message || '查询库文件失败')
						}
					},
					fail: (err) => {
						ElMessage.error('查询库文件失败')
						console.error('qryBaseFiles error:', err)
					}
				})
			}

			const openAddPage = () => {
				// 这里使用 uni.navigateTo 跳转到新增页面
				uni.navigateTo({
					url: './AddLibraryFile'
				})
			}

			onMounted(() => {
				fetchLibraryFiles()
			})

			return {
				libraryFiles,
				libraryFilesTransformed,
				openAddPage
			}
		}
	}
</script>

<style scoped>
	.library-manage {
		max-width: 800px;
		margin: 20px auto;
		padding: 20px;
	}

	.header-container {
		display: flex;
		justify-content: space-between;
		align-items: center;
		margin-bottom: 20px;
	}

	.header-left h2 {
		margin: 0;
		font-size: 22px;
		font-weight: bold;
	}

	.header-right {
		/* 可调整按钮样式 */
	}

	.table-section {
		margin-bottom: 20px;
	}
</style>